#encoding=UTF-8
import re,os,md5,cgi,logging,urllib
import wsgiref.handlers

from os import environ
from urllib import urlencode
from datetime import datetime
from datetime import date as strdate

from xml.dom import minidom
from django.utils import simplejson as json
from sgmllib import SGMLParser

from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.api import urlfetch

from inc.campus import Campus

class GWParser(SGMLParser):
  def reset(self):
    self.html = []
    self.isCondition = 0
    self.condition = "null"
    SGMLParser.reset(self)
    self.step = 0

  def start_b(self, attrs):
    if self.isCondition == 1:
      self.isCondition = 2
    self.unknown_starttag("b", attrs)
  
  def start_div(self, attrs):
    if self.step == 2:
      strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs])
      self.html.append("<div%(strattrs)s>" % locals())
      for att in attrs:
        if att[0] == "style" and att[1] == "font-size:84%":
          self.isCondition = 1
    else:
      for attr in attrs:
        if self.step == 0:
          if attr[0] == "class" and attr[1] == "e":
            self.step = 1

  def end_div(self):
    if self.step == 2:
      self.html.append("</div>" % locals())
    elif self.step == 3:
      self.step = 4

  def start_table(self, attrs):
    for attr in attrs:
      if attr[0] == "border" and attr[1] == "0":
        self.step = 2
        strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs])
        self.html.append("<table%(strattrs)s>" % locals())
  def end_table(self):
    if self.step == 2:
      self.html.append("</table>" % locals())
      self.step = 3
  
  def unknown_starttag(self, tag, attrs):
    if self.step == 2:
      strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs])
      self.html.append("<%(tag)s%(strattrs)s>" % locals())

  def unknown_endtag(self, tag):
    if self.step == 2:
      self.html.append("</%(tag)s>" % locals())

  def handle_data(self, text):
    if self.step == 2:
      self.html.append(text)
    if self.isCondition == 2:
      self.condition = text
      self.isCondition = 0

  def start_img(self, attrs):
    if self.step == 2:
      stratts = ""
      for attr in attrs:
        if attr[0] == "src":
          src = "http://www.google.cn" + attr[1]
          stratts = stratts + ' src="%s"' % src
        else:
          stratts = stratts + ' %s="%s"' % (attr[0], attr[1])
      self.html.append("<img%(stratts)s>" % locals())   

  def output(self):              
    return ["".join(self.html).decode("gb2312"), self.condition.decode("gb2312")]

#删除html里的>与<
def decI(str):
  start, end = str.find('>'), str.rfind('<')
  if (end == -1) and (start == -1):
    return str
  if(start == -1):
    start = 0
  if(end == -1):
    end = len(str)
  return decI(str[start+1:end])
 # return decI(str[start:end])

cnCity = ['北京', '上海', '天津', '重庆']
cnProvince = ['安徽', '河北', '山西','辽宁','吉林','黑龙江','江苏','浙江','福建','江西','山东','河南','湖北','广东','海南','四川','贵州','云南','陕西','甘肃','青海','台湾', '湖南']
cnAuto = ['内蒙古','广西','西藏','新疆', '宁夏', '香港','澳门']
cnAutoSuffix = ['','壮族','','维吾尔','回族','','']

cnProvinceCapital = ['合肥', '石家庄', '太原', '沈阳', '长春', '哈尔滨', '南京', '杭州', '福州', '南昌', '济南', '郑州', '武汉', '广州','海口','成都','贵阳', '昆明', '西安', '兰州' , '西安', '台北', '长沙']
cnAutoCapital = ['呼和浩特', '南宁', '拉萨', '乌鲁木齐', '银川']


def isProvinceCapital(cap):
  i = 0
  for capital in cnProvinceCapital:
    if(cap.find(capital) != -1):
      return cnProvince[i] + '省' + capital + "市"
    i = i + 1
  return None
 
def isAutoCapital(cap):
  i = 0
  for capital in cnAutoCapital:
    if(cap.find(capital) != -1):
      return cnAuto[i] + cnAutoSuffix[i] + '自治区' + capital + "市"
    i = i + 1
  return None

def getCapital(cap):
  if(isProvinceCapital(cap) != None):
    return isProvinceCapital(cap)
  if(isAutoCapital(cap) != None):
    return isAutoCapital(cap)
  return ""

def isCity(city):
  for cn in cnCity:
    if(city.find(cn) != -1):
      return cn + "市"
  return None

def isProvince(province):
  for cn in cnProvince:
    if(province.find(cn) != -1):
      return cn + "省"
  return None

def isAuto(auto):
  i = 0
  for cn in cnAuto:

    if(auto.find(cn) != -1):
      if(i >= 4):
        return cn + "特别行政区"
      return cn + cnAutoSuffix[i] + "自治区"
    i = i + 1
  return None

def isDivision(div):
  if(isProvince(div) != None):
    return isProvince(div)
  if(isCity(div) != None):
    return isCity(div)
  if(isAuto(div) != None):
    return isAuto(div)
  return ""


def findSub(str, div, sub):
  start = str.find(div)
  end = str.find(sub)
  if((end != -1) and (end - start > 3)):
    if(str[start + 3 : end] == "辖"):
      return None
    return str[start + 3: end + 3]
  elif(end == start + 3):
    return ""
  return None

def findSubDiv(div):
  if(isProvince(div) != None):
    start = '省'
    ends = ['市', '县', '区', '州']
    for end in ends:
      find = findSub(div, start, end)
      if(find != None):
        return find
    return ""
  if(isCity(div) != None):
    start = '市'
    ends = ['县', '区', '州']
    for end in ends:
      find = findSub(div, start, end)
      if(find != None):
        return find
    return ""
  if(isAuto(div) != None):
    start = '区'
    ends = ['州', '市', '县']
    for end in ends:
      find = findSub(div, start, end)
      if(find != None):
        return find
    return ""
  return ""


def fetchAlarmEx():
  base = 'http://search.weather.com.cn/static/list/warningList.php?page=1&pageSize=30'
  try:
    data = urlfetch.fetch(base).content
    ul = re.findall('<ul class="contentin_list">.*?</ul>', data, re.DOTALL)
    i = 0
    for u in ul:
      for li in re.findall("<li>.*?</li>", u, re.DOTALL):
        ass = re.findall('<a.*?</a>', li, re.DOTALL)
        info = {}
        for href in ass:
          ret = re.match('<a(.*?)href="(.*?)">(.*?)</a>', href)
          info['url'] = ret.group(2)
          md = md5.new()
          md.update(info['url'])
          info['id'] = md.hexdigest()
          info['info'] = ""
          info['source'] = "中国天气网"
          info['label'] = ret.group(3)
          info['region'] = isDivision(info['label'])
          if(len(info['region']) == 0):
            info['region'] = getCapital(info['label'])
            if(len(info['region']) == 0):
              info['region'] = "北京市"
        spans = re.findall('<span>.*?</span>', li, re.DOTALL)
        for span in spans:
          date = re.sub("<.*?>", "", span)
          date = date.split(" ")[0].strip()
          date = date.replace("-", "")
          info['date'] = date
        lat, lng = 0.0, 0.0
        cat = Category.update("天气预警信息".decode("UTF-8"), "ALARM")
        reg = Region.update(info['region'].decode("UTF-8"), lat, lng)
        item = CacheItem.update(info['id'], reg, cat, info['label'].decode("UTF-8"), info['date'], info['info'].decode("UTF-8"), info['source'].decode("UTF-8"), info['url'])
        i = i + 1
    return "%d"%(i)
  except:
    return

def fetchCities():
  f = open('city.txt', 'r')
  fstr = f.read()
  fstrarr = fstr.split("\n")
  f.close()
  cities = []
  for city in fstrarr:
    cityinfo = city.split(" ")
    if len(cityinfo) > 1:
      rank = int(cityinfo[1])
      if rank > 100 and getCapital(cityinfo[0]) == "" and isCity(cityinfo[0]) == None:
        cities.append(cityinfo[0])
  fstr = ""
  query = WeatherCities.all().fetch(1000)
  if len(query) > 50:
    return "failed, please run capital first"
  index = 50

  for city in cities:
    city = city[0:city.find("市")]
    wc = WeatherCities(name=city.decode("utf8"), id=index)
    index = index + 1
    wc.put()
  return "success"
def fetchCapital():
  db.delete(WeatherCities.all().fetch(1000))
  index = 0
  for city in cnCity:
    wc = WeatherCities(name=city.decode("utf8"), id=index)
    index = index + 1
    wc.put()
  for city in cnProvinceCapital:
    wc = WeatherCities(name=city.decode("utf8"), id=index)
    index = index + 1
    wc.put()
  for city in cnAutoCapital:
    wc = WeatherCities(name=city.decode("utf8"), id=index)
    index = index + 1
    wc.put()
  return "success"

def fetchTrain(url):
  base = 'http://www.chunyun.cn/news/2009linke/index'
  i = 0
  url = base + url;
  str = ""
  try:
    data = urlfetch.fetch(url).content 
  except:
    return 0
  divs = re.findall('<table border="0" cellspacing="0" cellpadding="0" width="100%">.*?</table>', data, re.DOTALL)
  for div in divs:
    tds = re.findall('<td>.*?</td>', div, re.DOTALL)
    inner = {}
    for td in tds:
#      print td.decode("GB2312").encode('UTF-8')
      hrefs = re.findall('href=".*?"', td, re.DOTALL)
      for href in hrefs:
        url = 'http://www.chunyun.cn' + href[6:len(href) - 1]
        inner['url'] = url
        inner['id'] = MD5(url)
        inner['source'] = "春运网临客信息"
#        print url
        break
      ass = re.findall('<A.*?</A>', td, re.DOTALL)
      for a in ass:
        a = re.sub("<.*?>", "", a)
        m = re.match("^.*?\((.*?)\).*?\((.*?)\).*?$", a)
        if(m != None):
#          print "m1= %s, m2 = %s" %( m.group(1).decode("GB2312").encode('UTF-8') , m.group(2).decode("GB2312").encode('UTF-8'))
          region = m.group(1).split("-")[0]
          inner['region'] = region.decode("GB2312").encode('UTF-8')
        else:
          inner['region'] = None
        inner['info'] = '<a href="' + inner['url'] + '" target="_blank">' + a.decode("GB2312").encode("UTF-8") + "</a>"
        inner['label'] = a.decode("GB2312").encode('UTF-8').replace("2009年", "").replace("临客时刻表", "").replace("春运", "").replace("2009", "").replace("临客时刻", "")
      dates = re.findall("<SPAN class=time>.*?</SPAN>", td, re.DOTALL)
      for date in dates:
        date = re.sub("<.*?>", "", date)
        date = date.replace("&nbsp;", "")
        date = date.replace("-", "")
        date = "2009" + date
        inner['date'] = date
#        print date.decode("GB2312").encode('UTF-8')
      if inner['region'] == None:
        continue
      lat, lng = 0.0, 0.0
      reg = Region.update(inner['region'].decode("UTF-8"), lat, lng)
      cat = Category.update("临客信息".decode("UTF-8"), "TRAIN")
      item = CacheItem.update(inner['id'], reg, cat, inner['label'].decode("UTF-8"), inner['date'], inner['info'].decode("UTF-8"), inner['source'].decode("UTF-8"), inner['url'])
      i = i + 1
  return i


def fetchRoad(url):
  base = "http://www.moc.gov.cn/zhuzhan/gongluchuxing/lukuangxinxi/index";
  results = []
  url = base + url;
  str = ""
  data = urlfetch.fetch(url).content 
  divs = re.findall('<table width="556" border="0" cellspacing="0" cellpadding="0">.*?</table>', data, re.DOTALL)
  for div in divs:
    tds = re.findall('<td width="405" height="25" align="left" bgcolor="#FFFFFF">.*?</td>', div, re.DOTALL)
    inner = {}
    for td in tds:
      hrefs = re.findall('href=".*?target="_blank"', td, re.DOTALL)
      for href in hrefs:
        id = href[8:len(href) - 22]
        id = id[id.rfind('/') + 1:]
        inner['id'] = id
        href = "http://www.moc.gov.cn/zhuzhan/gongluchuxing/lukuangxinxi/" + href[8:len(href) - 17];
        inner['info'] = ""
        inner['source'] = '交通部路况信息'
        inner['url'] = href;
        break
      for label in re.findall('title=".*?"', td, re.DOTALL):
        label = label[7:len(label) - 1]
        break
      inner['label'] = label
      inner['region'] = isDivision(td) + findSubDiv(td)
    tds = re.findall('<td width="91" align="left" class="black12">.*?</td>', div, re.DOTALL)
    for td in tds:
      td = decI(td)
      td = td[1:len(td) - 1]
      inner['date'] = td.replace("-", "")
      break
    if(inner != {}):
      a = inner['info']
      lat, lng = 0.0, 0.0
      reg = Region.update(inner['region'].decode("UTF-8"), lat, lng)
      cat = Category.update("公路路况信息".decode("UTF-8"), "ROAD")
      str = str + "info = " + inner['id'] + inner['region'] +  "公路路况信息" + inner['label'] +  inner['date'] +  inner['info'] +  inner['source'] + inner['url'] + "\r\n"
      item = CacheItem.update(inner['id'], reg, cat, inner['label'].decode("UTF-8"), inner['date'], inner['info'].decode("UTF-8"), inner['source'].decode("UTF-8"), inner['url'])
      results.append(inner)
  return len(results)

class WeatherCities(db.Model):
  id = db.IntegerProperty()
  name = db.StringProperty()
  lat = db.FloatProperty()
  lng = db.FloatProperty()
  @staticmethod
  def get(name):
    regions = db.GqlQuery("SELECT * FROM WeatherCities WHERE name = :1 ", name)
    reg = regions.get()
    return reg

class Region(db.Model):
  id = db.StringProperty()
  name = db.StringProperty()
  lat = db.FloatProperty()
  lng = db.FloatProperty()
  @staticmethod
  def get(name):
    regions = db.GqlQuery("SELECT * FROM Region WHERE name = :1 ", name)
    reg = regions.get()
    return reg
  @staticmethod
  def update(name, lat, lng):
    reg = Region.get(name)
    if(reg == None):
      reg = Region()
      reg.name = name
      reg.lat = lat
      reg.lng = lng
    if reg.lat == 0:
      reg.lat = lat
    if reg.lng == 0:
      reg.lng = lng
    reg.put()
    return reg
  
class Cities(db.Model):
  id = db.StringProperty()
  name = db.StringProperty()
  lat = db.FloatProperty()
  lng = db.FloatProperty()

class Weather(db.Model):
  id = db.IntegerProperty()
  city = db.StringProperty()
  info = db.TextProperty()
  lat = db.FloatProperty()
  lng = db.FloatProperty()
  condition = db.StringProperty()
  @staticmethod
  def get(name):
    regions = db.GqlQuery("SELECT * FROM Weather WHERE city = :1 ", name)
    reg = regions.get()
    return reg

class Category(db.Model):
  id = db.StringProperty()
  name = db.StringProperty()
  @staticmethod
  def getId(id):
    cats = db.GqlQuery("SELECT * FROM Category WHERE id = :1 ", id)
    cat = cats.get()
    return cat
  @staticmethod
  def get(name):
    regions = db.GqlQuery("SELECT * FROM Category WHERE name = :1 ", name)
    reg = regions.get()
    return reg
  @staticmethod
  def update(name, id):
    reg = Category.get(name)
    if(reg == None):
      reg = Category()
      reg.name = name
    reg.id  = id
    reg.put()
    return reg



class Item(db.Model):
  id = db.StringProperty()
  region = db.ReferenceProperty(Region)
  category = db.ReferenceProperty(Category)
  label = db.StringProperty()
  date = db.StringProperty()
  info = db.StringProperty()
  source = db.StringProperty()
  url = db.StringProperty()
  @staticmethod
  def get(id):
    items = db.GqlQuery("SELECT * FROM Item WHERE id = :1 ", id)
    item = items.get()
    return item
  @staticmethod
  def update(id, region, category, label, date, info, source, url):
    item = Item.get(id)
    if(item == None):
      item = Item()
      item.id = id
    item.region = region
    item.category = category
    item.label = label
    item.date = date
    item.info = info
    item.source = source
    item.url = url
    item.put()

class CacheItem(db.Model):
  id = db.StringProperty()
  region = db.ReferenceProperty(Region)
  category = db.ReferenceProperty(Category)
  label = db.StringProperty()
  date = db.StringProperty()
  info = db.StringProperty()
  source = db.StringProperty()
  url = db.StringProperty()
  @staticmethod
  def get(id):
    items = db.GqlQuery("SELECT * FROM CacheItem WHERE id = :1 ", id)
    item = items.get()
    return item
  @staticmethod
  def update(id, region, category, label, date, info, source, url):
    item = CacheItem.get(id)
    if(item == None):
      item = CacheItem()
      item.id = id
    item.region = region
    item.category = category
    item.label = label
    item.date = date
    item.info = info
    item.source = source
    item.url = url
    item.put()

def log(str):
  logging.debug(str)

def MD5(str):
  md = md5.new()
  md.update(str)
  return md.hexdigest()

def findInArr(arr, name):
  for ele in arr:
    if ele == name:
      return True
  return False

class MainPage(webapp.RequestHandler):
  tpath = os.path.dirname(__file__)
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Welcome to the shareinmap project')

  def post(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Hello, webapp World! Post')
  class UnaryPage(webapp.RequestHandler):
    def get(self, module):
      if module == "campus":
        path = os.path.dirname(__file__) + "/template/";
        campus = Campus(self, 'get', {}, path)
      if module == "capital":
        self.response.out.write(fetchCapital())
      if module == "city":
        self.response.out.write(fetchCities())
      if module == "alarmEx":
        try:
          str = fetchAlarmEx()
        except:
          str = "0"
        self.response.out.write(str)
        return
      if module == "train":
        urls = ['.html', '_2.html', '_3.html', '_4.html', '_5.html']
        try:
          page = self.request.get('page')
          page = int(page)
        except:
          page = 0
        if(page > 4) or (page < 0):
          self.response.out.write("range is to big")
          return
        if(page == 0):
          cat = Category.update("临客信息".decode("UTF-8"), "TRAIN")
          db.delete(CacheItem.all().filter("category = ", cat))
        str = fetchTrain(urls[page])
        self.response.out.write(str)
        return
      if module == "road":
        urls = ['.html', '_1.html', '_2.html', '_3.html', '_4.html']
        try:
          page = self.request.get('page')
          page = int(page)
        except:
          page = 0
        if(page > 4) or (page < 0):
          self.response.out.write("range is to big")
          return
        if(page == 0):
          cat = Category.update("公路路况信息".decode("UTF-8"), "ROAD")
          db.delete(CacheItem.all().filter("category = ", cat))
        str = fetchRoad(urls[page])
        self.response.out.write(str)
        return
      if module == "index":
        self.response.out.write(template.render('Chunyun/chunyun-map.html', {}))
        return

      if module == "delete":
        return
        for item in Item.all():
          item.delete()
        for item in CacheItem.all():
          item.delete()
        return
      if module == "updateAlarm":
        cat = Category.update("天气预警信息".decode("UTF-8"), "ALARM")
        db.delete(Item.all().filter("category =", cat))
        i = 0
        for item in CacheItem.all().filter("category =", cat):
          Item.update(item.id, item.region, item.category, item.label, item.date, item.info, item.source, item.url)
          i = i + 1
        self.response.out.write("1")
        return
      if module == "updateTrain":
        page = self.request.get('page')
        cat = Category.update("临客信息".decode("UTF-8"), "TRAIN")
        try:
          page = int(page)
        except:
          page = 0
        if page == 0:
          db.delete(Item.all().filter("category =", cat))
        step = 40
        for item in CacheItem.all().filter("category =", cat).fetch(step, page * step):
          Item.update(item.id, item.region, item.category, item.label, item.date, item.info, item.source, item.url)
        self.response.out.write("1")
        return
        
      if module == "updateRoad":
        page = self.request.get('page')
        cat = Category.update("公路路况信息".decode("UTF-8"), "ROAD")
        try:
          page = int(page)
        except:
          page = 0
        if page == 0:
          db.delete(Item.all().filter("category =", cat))
        step = 15
        for item in CacheItem.all().filter("category =", cat).fetch(step, page * step):
          Item.update(item.id, item.region, item.category, item.label, item.date, item.info, item.source, item.url)
        self.response.out.write("1")
        return
      if module == "update":
        i = 0
        road = 0
        alarm = 0
        cat = Category.update("天气预警信息".decode("UTF-8"), "ALARM")

        for item in CacheItem.all():
          if(item.category.id == "ALARM"):
            alarm = alarm + 1
          if(item.category.id == "ROAD"):
            road = road + 1
          if(item.info == ""):
            self.response.out.write("0")
            return
          i = i + 1
        if(alarm != 0):
          for item in Item.all():
            if(item.category.id == "ALARM"):
              item.delete()
          for item in CacheItem.all():
            if(item.category.id == "ALARM"):
              Item.update(item.id, item.region, item.category, item.label, item.date, item.info, item.source, item.url)
        if(road >= 20):
          for item in Item.all():
            if(item.category.id == "ROAD"):
              item.delete()
          for item in CacheItem.all():
            if(item.category.id == "ROAD"):
              Item.update(item.id, item.region, item.category, item.label, item.date, item.info, item.source, item.url)
        if(alarm == 0):
          self.response.out.write("2")
          return
        if(road < 20):
          self.response.out.write("3")
          return
        self.response.out.write("1")
        return
      if module == "item":
        i = 1
        self.response.out.write("Item starts here:")
        for item in Item.all():
          self.response.out.write(
            "No %d : id = %s, region = %s, cat = %s, label = %s, date = %s, info = %s, source = %s, url = %s<br/>\r\n"
            % (i,
            item.id,
            item.region.name + ":%f, %f" % (item.region.lat, item.region.lng),
            item.category.name + ":" + item.category.id,
            item.label,
            item.date,
            item.info,
            item.source,
            item.url))
          i = i + 1
        i = 1
        self.response.out.write("CacheItem starts here:")
        for item in CacheItem.all():
          self.response.out.write(
            "No %d : id = %s, region = %s, cat = %s, label = %s, date = %s, info = %s, source = %s, url = %s<br/>\r\n"
            % (i,
            item.id,
            item.region.name + ":%f, %f" % (item.region.lat, item.region.lng),
            item.category.name + ":" + item.category.id,
            item.label,
            item.date,
            item.info,
            item.source,
            item.url))
          i = i + 1
 
      if module == "count":
        count = 0
        count = len(CacheItem.all().filter("info = ", "").fetch(1000))
        self.response.out.write("%d"%(count))
        return
      if module == "view":
        i = 1
        self.response.headers['Cache-Control'] = "no-cache"
        for item in CacheItem.all().filter("info = ", ""):
          if(item.category.id == "ALARM"):
            #self.response.out.write("matched\r\n")
            if item.url.startswith("http://www.weather.com.cn"):
              try:
                indata = urlfetch.fetch(item.url).content
              except:
                continue
              for info in re.findall('<dl id="mainContent" >.*?</dl>', indata, re.DOTALL):
                info = info[len('<dl id="mainContent" >'):len(info) - 5].strip()
                info = info.replace(" ", "")
                info = info.replace("\r", "")
                info = info.replace("\n", "")
                info = info.replace("\t", "")
                info = info.decode("UTF-8")
                item.info = info[:500]
                item.put()
                self.response.out.write('<meta http-equiv="refresh" content="0">')
                return
              continue
          if(item.category.id == "ROAD"):
            try:
              indata = urlfetch.fetch(item.url).content
            except:
              self.response.out.write("error fetch content")
              self.response.out.write(
                "No %d : id = %s, region = %s, cat = %s, label = %s, date = %s, info = %s, source = %s, url = %s<br/>\r\n"
                % (i,
                item.id,
                item.region.name + ":%f, %f" % (item.region.lat, item.region.lng),
                item.category.name + ":" + item.category.id,
                item.label,
                item.date,
                item.info,
                item.source,
                item.url))
              break
              continue
            styles = re.findall('<style id=_Custom_Style_>.*?</font', indata, re.DOTALL)
            for style in styles:
              style = style[style.find('/') + 8: len(style) - 6]
              style = style.replace("<.*?>", "")
              info = style.decode("UTF-8")
              item.info = info[:500]
              item.put()
              self.response.out.write('<meta http-equiv="refresh" content="0">')
              return
        return
      if module == "region":
        for region in Region.all():
          if(region.name == ""):
            region.delete()
            continue
          self.response.out.write("name = %s, lat = %f, lng = %f<br/>\r\n" % (region.name, region.lat, region.lng))
        return
      if module == "category":
        cat = Category.update("临客信息".decode("UTF-8"), "TRAIN")
        for category in Category.all():
          if(category.name == ""):
            category.delete()
            continue
          self.response.out.write("name = %s, id = %s<br/>\r\n" % (category.name, category.id))
        return
      if module == "chunyun":
        if(self.request.get('api') == 'searchAll'):
          key = self.request.get('key')
          page = self.request.get('page')
          callback = self.request.get('callback')
          try:
            page = int(page)
          except:
            page = 0

          items = []
          i = 0
          step = 15
          offset = page * step 
          for item in Item.all():
            isLabel = item.label.find(key) != -1
            isInfo = item.info.find(key) != -1
            isRegion = item.region.name.find(key) != -1
            isCategory = item.category.name.find(key) != -1
            val = isLabel or isInfo or isRegion or isCategory
            if(val):
              offset = page * step 
              if( i >= offset):
                items.append(item)
              i = i + 1
          rets = []
          for item in items:
            source = {
              'label': item.source,
              'url': item.url
            }
            latlng = None
            if(item.region.lat != 0 and item.region.lng != 0):
              latlng = [item.region.lat, item.region.lng]
            ret = {
              'label': item.label,
              'date': item.date,
              'info': item.info,
              'source': source,
              'region': item.region.name,
              'latlng': latlng
            }
            rets.append(ret)
          total = i / step
          remainder = i % step
          if(remainder != 0):
            total = total + 1
          info = {
            'total': total,
            'page': page,
            'result': rets
          }
          self.response.out.write(callback + "(" + json.dumps(info) + ")")
          return
        if(self.request.get('api') == 'options'):
          weas = []
          als = []
          rds = []
          trains = []
          query = Weather.all()
          query.order("id")
          for wea in query.fetch(250):
            if findInArr(weas, wea.city) == False:
              weas.append(wea.city)
          query = Item.all()
          for item in query.fetch(250):
            if item.category.id == "ALARM":
              if findInArr(als, item.region.name) == False:
                als.append(item.region.name)
            elif item.category.id == "ROAD":
              if findInArr(rds, item.region.name) == False:
                rds.append(item.region.name)
            elif item.category.id == "TRAIN":
              if findInArr(trains, item.region.name) == False:
                trains.append(item.region.name)
          ret = {'roadregion':rds, 'wearegion': weas, 'alarmregion': als, 'trainregion': trains}
          self.response.out.write(self.request.get("callback") + "(" + json.dumps(ret) + ")")
        elif self.request.get('api') == "search":
          step = 15
          page = self.request.get("page")
          callback = self.request.get("callback")
          region = self.request.get('region')
          category = self.request.get('category')
          date = self.request.get('date')
          inc = self.request.get('inc')
          if page != "":
            page = int(page)
          else:
            page = 0
          items = []
          offset = page * step 
          cat = Category.getId(category)
          model = Item.all().filter("category = ", cat)
          if(region != ""):
            model = model.filter("region = ", Region.get(region))
          if(date != ""):
            if(inc == "1"):
              model = model.filter("date >= ", date)
            else:
              model = model.filter("date = ", date)
          i = len(model.fetch(1000))
          for item in model.order("-date").fetch(step, offset):
            items.append(item)
          rets = []
          for item in items:
            source = {
              'label': item.source,
              'url': item.url
            }
            latlng = None
            if(item.region.lat != 0 and item.region.lng != 0):
              latlng = [item.region.lat, item.region.lng]
            ret = {
              'label': item.label,
              'date': item.date,
              'info': item.info,
              'source': source,
              'region': item.region.name,
              'latlng': latlng
            }
            rets.append(ret)
          total = i / step
          remainder = i % step
          if(remainder != 0):
            total = total + 1
          info = {
            'total': total,
            'page': page,
            'cata': category,
            'result': rets
          }
          self.response.out.write(callback + "(" + json.dumps(info) + ")")
        elif self.request.get('api') == 'wgeocode':
          reg = WeatherCities.get(self.request.get('name'))
          latlng = self.request.get('latlng').split(',')
          lat = float(latlng[0])
          lng = float(latlng[1])
          if(reg != None):
            reg.lat = lat
            reg.lng = lng
            reg.put()
            #self.response.out.write("write city")
          reg = Weather.get(self.request.get('name'))
          if(reg != None):
            reg.lat = lat
            reg.lng = lng
            reg.put()
            #self.response.out.write("write city")
        elif self.request.get('api') == 'geocode':
          reg = Region.get(self.request.get('name'))
          if(reg != None):
            latlng = self.request.get('latlng').split(',')
            reg.lat = float(latlng[0])
            reg.lng = float(latlng[1])
            reg.put()
        elif self.request.get('api') == 'weather':
          callback = self.request.get("callback")
          city = self.request.get("region")
          page = self.request.get("page")
          if page == "":
            page = 0
          else:
            page = int(page)
          wquery = Weather.all()
          if city != "null" and city != "":
            wquery.filter("city = ", city)
          else:
            wquery.order("id")
          try:
            wres = wquery.fetch(1000);
          except:
            self.response.out.write(city)
            return
          total = len(wres)
          tp = (total - total % 15) / 15
          if total % 15 != 0:
            tp += 1
          str = ""
          start = page * 15
          end = (page + 1) * 15
          wcount = 0
          first = True
          for wre in wres:
            if wcount < start or wcount > end:
              wcount = wcount + 1
              continue
            if first != True:
              str += ", "
            first = False
            wcount = wcount + 1
            objjson = {
              'city': wre.city,
              'lat': wre.lat,
              'lng': wre.lng,
              'info': wre.info,
              'condition': wre.condition
              }
            str += json.dumps(objjson)
          self.response.out.write("%s({'total':%d,'page':%d,'result':[%s]})"%(callback, tp, page, str))
        else:
          self.response.out.write(template.render('Chunyun/chunyun-map.html', {}))
        return

      if module == "we":
        cities = WeatherCities.all()
        cities.order("id")
        cities = cities.fetch(1000)
        index = int(self.request.get("index"))
        callback = self.request.get("callback")
        if index == 0:
          db.delete(Weather.all().fetch(1000))
        if index >= len(cities):
          self.response.out.write(callback + "(-1)")
          return
        city = cities[index].name
        lat = cities[index].lat
        lng = cities[index].lng
        cid = cities[index].id
        extend = (city + "+天气".decode("utf8")).encode('utf8')
        extend = urlencode({"q": extend})
        baseurl = 'http://www.google.cn/search?hl=zh-CN&%s'%extend
#        baseurl = urlencode(baseurl)
        try:
          result = urlfetch.fetch(baseurl)
        except:
          self.response.out.write(callback + "(%d, 'noresult')"%(index + 1))
          return
        if result.status_code == 200:
          gw = GWParser()
          gw.feed(result.content)
          infos = gw.output()
          if infos[0] == "" or infos[1] == "":
            self.response.out.write(callback + "(%d, '%s noresult')"%(index + 1, city))
            return
          we = Weather.get(city)
          if we == None:
            we = Weather(id=cid, city=city, lat=lat, lng=lng, info=infos[0], condition=infos[1])
          else:
            we.info = infos[0]
            we.condition = infos[1]
          we.put()
#          self.response.out.write(infos[0])
          self.response.out.write(callback + "(%d, '%s')"%(index+1, infos[0]))
        else:
          self.response.out.write(callback + "(%d, 'noresult')"%(index + 1))
          
      if module == "wea":
        page = self.request.get("page")
        if page == "":
          page = 0
        else:
          page = int(page)
        count = 10
        page = count * page
        baseurl = 'http://www.google.com/ig/api?hl=zh_CN&weather='
        query = Cities.all()
        cities = query.fetch(count, page)
        cits = []
        if page == 0:
          db.delete(Weather.all().fetch(100))
        str = ""
        for city in cities:
          fetched = True
          while fetched:
            result = urlfetch.fetch(baseurl + city.id)
            if result.status_code == 200:
              fetched = False
              weather = minidom.parseString(result.content.decode('gb2312').encode('utf8'))
              curs = weather.getElementsByTagName("current_conditions")
              currents = []
              for ele in curs:
                currents.append({
                    'condition': ele.getElementsByTagName("condition")[0].attributes["data"].value,
                    'temp_f': ele.getElementsByTagName("temp_f")[0].attributes["data"].value,
                    'temp_c': ele.getElementsByTagName("temp_c")[0].attributes["data"].value,
                    'humidity': ele.getElementsByTagName("humidity")[0].attributes["data"].value,
                    'icon': ele.getElementsByTagName("icon")[0].attributes["data"].value,
                    'wind': ele.getElementsByTagName("wind_condition")[0].attributes["data"].value
                  })
              fors = weather.getElementsByTagName("forecast_conditions")
              forecasts = []
              for ele in fors:
                forecasts.append({
                  'dow': ele.getElementsByTagName("day_of_week")[0].attributes["data"].value,
                  'low': ele.getElementsByTagName("low")[0].attributes["data"].value,
                  'high': ele.getElementsByTagName("high")[0].attributes["data"].value,
                  'icon': ele.getElementsByTagName("icon")[0].attributes["data"].value,
                  'condition': ele.getElementsByTagName("condition")[0].attributes["data"].value
                })
              cit ={
                  'city': city.name,
                  'lat': city.lat,
                  'lng': city.lng,
                  'current': currents,
                  'forecast': forecasts
              }
              wstr = json.dumps(cit)
              wquery = Weather.all()
              wquery.filter('city =', city.name)
              wres = wquery.fetch(100)
              if len(wres) != 0:
                wres[0].json = str;
                wres[0].put()
              else:
                wea = Weather(city = city.name, json = wstr)
                wea.put()
                
              str += wstr
        self.response.out.write(str)
        return
      if module == "cities":
        db.delete(Cities.all().fetch(1000))
        cities = minidom.parse("cities.xml")
        names = cities.getElementsByTagName("name");
        lats = cities.getElementsByTagName("latitude_e6");
        lngs = cities.getElementsByTagName("longitude_e6");
        for i in range(len(names)):
          city = Cities()
          city.id = names[i].attributes["data"].value;
          city.name = names[i].attributes["cn"].value;
          city.lat = float(lats[i].attributes["data"].value)/1000000;
          city.lng = float(lngs[i].attributes["data"].value)/1000000;
          city.put()
        self.response.out.write("success!")
        return
        
    def post(self, module):
      method = "post"
      if module == "campus":
        campus = Campus(self, method, {})
 
      return
        
  class BinaryPage(webapp.RequestHandler):
    def get(self, module, act):
      return
    def post(self, module, act):
      return
  class TernaryPage(webapp.RequestHandler):
    def get(self, module, act, param):
      return
    def post(self):
      return
def main():
  application = webapp.WSGIApplication(
    [
    ('/', MainPage),
    ('/(.*)/(.*)/(.*)', MainPage.TernaryPage),
    ('/(.*)/(.*)', MainPage.BinaryPage),
    ('/(.*)', MainPage.UnaryPage),
    ],
      debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
